package org.dandsoft.blackbox;

import java.io.Serializable;

/**
 * @author Vilyuzhanin Denis (Fin)
 */
public class Signal implements Serializable {

    private SignalId signalId;
    private int value;

    public Signal(int value) {
        this.value = value;
    }

    public Signal(SignalId signalId, int value) {
        this.signalId = signalId;
        this.value = value;
    }

    public synchronized void setSignalId(SignalId id) {
        if (signalId == null) {
            signalId = id;
        }
    }

    public boolean isEquelValues(Signal signal) {
        return this.value == signal.value;
    }

    public boolean isAnonymous() {
        return signalId == null;
    }

    public SignalId getSignalId() {
        return signalId;
    }

    public int getValue() {
        return value;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder();
        sb.append("Signal");
        sb.append("{signalId=").append(signalId);
        sb.append(", value=").append(value);
        sb.append('}');
        return sb.toString();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Signal signal = (Signal) o;

        if (value != signal.value) return false;
        if (signalId != null ? !signalId.equals(signal.signalId) : signal.signalId != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = signalId != null ? signalId.hashCode() : 0;
        result = 31 * result + value;
        return result;
    }
}
